package demo1.web.servlet.userBuyapi;

import demo1.dao.Mapper.userBuyMapper;
import demo1.pojo.userBuy;
import demo1.dao.sql.userBuysql;
import demo1.service.userBuyService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

@WebServlet("/userBuyLogin")
public class userBuyLogin extends HttpServlet {//展示用户登录的页面
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String userName = req.getParameter("userName");
        String userPwd = req.getParameter("userPwd");
        resp.setContentType("text/html;charset = utf-8");
        PrintWriter writer = resp.getWriter();

        try {
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //判断用户是否登录成功
        try {
            userBuyService login = new userBuyService();
            int num = login.checkLogin(userName,userPwd);
            if(num == 1){
                //用户存在,登录成功
                userBuy users = new userBuy();//写入用户
                users = login.writeUser(userName,userPwd);
                HttpSession session = req.getSession();
                session.setAttribute("users", users);
                resp.sendRedirect("/logindemo2/showUserBuy.jsp");
            }
            else {
                writer.write("登录失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
